﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Globalization;

namespace DigitalSolutionControls
{
    public class DateEdit:TextBox
    {
        private string timeSeparator;
        private string shortDatePattern;
        
        public DateTime Value
        {
            get
            {
                DateTime date;
                DateTime.TryParseExact(this.Text, shortDatePattern, CultureInfo.CurrentCulture, DateTimeStyles.None, out date);
                return date;
            }
            set
            {
                if (value != DateTime.MinValue)
                {
                    base.Text = value.ToShortDateString();
                    this.ForeColor = Color.Black;
                }
            }
        }

        public bool Empty
        {
            get
            {
                return this.Text == shortDatePattern;
            }
        }


        public DateEdit()
        {
            this.MinimumSize = new Size(24, 24);
            this.Font = Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.AutoSize = false;
            this.ForeColor = Color.Gray;
            timeSeparator = CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator;
            shortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
            this.ShortcutsEnabled = false;
            this.MaxLength = 10;
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = shortDatePattern;
            }
        }



        public override int MaxLength
        {
            get
            {
                return base.MaxLength;
            }
            set
            {
                base.MaxLength = 10;
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (!Char.IsControl(e.KeyChar) && !Char.IsNumber(e.KeyChar) && e.KeyChar.ToString() != timeSeparator)
                e.Handled = true;
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this.ForeColor = Color.Black;
            if (this.Text == shortDatePattern)
                base.Text = String.Empty;
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            if (this.Text == String.Empty)
            {
                base.Text = shortDatePattern;
                this.ForeColor = Color.Gray;
            }
        }

        public bool IsValidDate()
        {
            DateTime outDate;
            return DateTime.TryParseExact(this.Text, shortDatePattern, CultureInfo.CurrentCulture, DateTimeStyles.None, out outDate);
        }
    }
}
